﻿using CKS2320.Common.Model;
using CKS2320.Common.Transfer;
using CKS2320.Common.Utils;
using CKS2320.Domain.DBEntity;
using CKS2320.Repository.Interface;
using CKS2320.Service.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CKS2320.Service
{
    public class EquipmentfailureService : IEquipmentfailureService
    {
        public IEquipmentfailureRepository _equipmentfailureRepository;
        public EquipmentfailureService(IEquipmentfailureRepository equipmentfailureRepository)
        {
            _equipmentfailureRepository = equipmentfailureRepository;
        }
        /// <summary>
        /// 获取故障码表
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public List<EquipmentfailureModel> GetEquipmentfailures(string ip, string Language = "CN")
        {
            List<EquipmentfailureModel> result = new List<EquipmentfailureModel>();
            var dbEntity = _equipmentfailureRepository.GetEquipmentfailures(ip, Language);
            if (dbEntity != null && dbEntity.Any())
            {
                foreach (var item in dbEntity)
                {
                    result.Add(new EquipmentfailureModel()
                    {
                        Id = item.Id,
                        Content = item.Content,
                        FailureIndex = item.FailureIndex,
                        FailureOffset = item.FailureOffset,
                        Plcip = item.Plcip,
                        Language = item.Language
                    });
                }
            }
            return result;
        }
        /// <summary>
        /// 插入一条故障记录
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public ResultReturn InsertHistory(EquipmentfailurehistoryModel req)
        {
            if (req != null)
            {
                Equipmentfailurehistory equipmentfailurehistory = new()
                {
                    Id = Guid.NewGuid(),
                    FailureId = req.FailureId.Value,
                    Content = req.Content,
                    FaultType = req.FaultType,
                    StartTime = req.StartTime,
                    EndTime = req.EndTime,
                    TotalTime = req.TotalTime,
                    CreatedTime = DateTime.Now,
                    ModifiedTime = DateTime.Now
                };
                return _equipmentfailureRepository.InsertHistory(equipmentfailurehistory);
            }
            return MessageInfoUtils.Fail();
        }
        /// <summary>
        /// 更新一条故障记录
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public ResultReturn EditHistory(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                var entity = _equipmentfailureRepository.GetById(id);
                if (entity != null)
                {
                    DateTime now = DateTime.Now;
                    string totalTime = string.Empty;
                    if (entity.StartTime != null)
                    {
                        totalTime = Math.Round((now - (DateTime)entity.StartTime).TotalMinutes, 2).ToString();
                    }
                    entity.FaultType = false;
                    entity.EndTime = now;
                    entity.TotalTime = totalTime;
                    entity.ModifiedTime = DateTime.Now;
                    return _equipmentfailureRepository.Edit(entity);
                }
            }
            return MessageInfoUtils.Fail();
        }
        /// <summary>
        /// 获取历史故障记录
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public ResultReturn GetEquipmentfailureHistory(string Language = "CN")
        {
            try
            {
                DateTime historyDate = Convert.ToDateTime(DateTime.Now.AddDays(-5).ToString("yyyy-MM-dd") + " 00:00:00");
                var history = _equipmentfailureRepository.GetEquipmentfailureHistory(historyDate, "CN");
                if (history != null && history.Any())
                {
                    foreach (var item in history)
                    {
                        //item.Content = TransferDic.GetMessage(item.Content, Language);
                        //item.equipmentFailureInfo.Content = TransferDic.GetMessage(item.equipmentFailureInfo.Content, Language);
                        item.Content = TransferDic.GetlanguageText(item.Content, Language);
                        item.equipmentFailureInfo.Content = TransferDic.GetlanguageText(item.equipmentFailureInfo.Content, Language);
                        
                    }
                    return MessageInfoUtils.Success(history);
                }
            }
            catch (Exception ex)
            {
                return MessageInfoUtils.Fail(ex.Message);
            }
            return MessageInfoUtils.Success();
        }
        /// <summary>
        /// 获取正在故障阶段的记录
        /// </summary>
        /// <param name="Language"></param>
        /// <returns></returns>
        public IEnumerable<EquipmentfailurehistoryModel>? GetFailureHistory(string ip, string Language = "CN")
        {
            try
            {
                var history = _equipmentfailureRepository.GetFailureHistory(ip, Language);
                if (history != null && history.Any())
                {
                    return history;
                }
            }
            catch (Exception ex) { }
            return null;
        }
    }
}
